package com.waterworld.watch.home.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.RemotePhotoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.adapter.RemotePhotoAdapter;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;
import com.waterworld.watch.push.contant.MessageContants;
import com.waterworld.watch.push.event.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SkillRemoteShotActivity extends BaseActivity{


    @BindView(R.id.header_parent)
    LinearLayout headerParent;

    @BindView(R.id.header_title)
    TextView headerTitle;

    @BindView(R.id.header_back)
    ImageView headerBack;

    @BindView(R.id.btn_remote_shot)
    Button btn_remote_shot;

    @BindView(R.id.rv_remote_photo)
    RecyclerView rv_remote_photo;

    private static final int START_REMOTE_PHOTO = 0X01;
    private static final int END_REMOTE_PHOTO = 0X02;

    private List<RemotePhotoBean> mList = new ArrayList<>();
    private RemotePhotoAdapter mAdapter;

    private IHomeManager iHomeManager = HomeManager.getInstance();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skill_shot);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case START_REMOTE_PHOTO:
                    btn_remote_shot.setClickable(false);
                    btn_remote_shot.setText(getString(R.string.shooting));
                    btn_remote_shot.setBackground(getResources().getDrawable(R.drawable.upload_button_pressed));
                    break;
                case END_REMOTE_PHOTO:
                    btn_remote_shot.setClickable(true);
                    btn_remote_shot.setText(R.string.remote_shot);
                    btn_remote_shot.setBackground(getResources().getDrawable(R.drawable.upload_button));
            }
        }
    };

    private void initView() {
        setViewSize(headerParent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        headerTitle.setText(getString(R.string.remote_shot));
        headerBack.setVisibility(View.VISIBLE);
        headerTitle.setVisibility(View.VISIBLE);
        getListPhoto();

    }

    @OnClick(R.id.btn_remote_shot)
    void onRemoteShot() {
        if (iHomeManager != null) {
            iHomeManager.remotePhoto(new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        mHandler.sendEmptyMessage(START_REMOTE_PHOTO);
                    }
                }
            });
        }
    }

    private void getListPhoto() {
        if (iHomeManager != null) {
            iHomeManager.listPhoto(1, 100, new BaseObserverListener<BaseResultBean<RemotePhotoBean[]>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(SkillRemoteShotActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<RemotePhotoBean[]> baseResultBean) {
                    if (baseResultBean.getData().length != 0) {
                        mList.clear();
                        mList.addAll(Arrays.asList(baseResultBean.getData()));
                        mAdapter = new RemotePhotoAdapter(SkillRemoteShotActivity.this,mList);
                        rv_remote_photo.setLayoutManager(new GridLayoutManager(SkillRemoteShotActivity.this,4));
                        rv_remote_photo.setAdapter(mAdapter);
                    }
                }
            });
        }
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void remoteShotResult(MessageEvent event) {
        mHandler.sendEmptyMessage(END_REMOTE_PHOTO);
        if (event.getType() == MessageContants.UPLOAD_PHOTO_OK) {
            getListPhoto();
        } else if (event.getType() == MessageContants.UPLOAD_PHOTO_FAIL) {
            ToastUtils.showShort(this,"远程拍照失败~");
        }
    }
}
